#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/mman.h>
#include "chumo.h"
#include "font.h"

int read_JPEG_file(char *filename, void *map_addr, int px, int py);

int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        printf("请输入需要显示的图片\n");
        return -1;
    }
    // 1.打开LCD设备文件 (LCD是开发版的屏幕)
    int lcd_fd = open("/dev/fb0", O_RDWR);
    if (lcd_fd < 0)
    {
        perror("打开LCD设备失败\n");
        return -1;
    }
    else
    {
        printf("打开LCD设备成功\n");
    }

    // 2.映射LCD 设备
    void *map_addr = mmap(NULL, 800 * 480 * 4, PROT_READ | PROT_WRITE, MAP_SHARED, lcd_fd, 0);
    if (map_addr == MAP_FAILED)
    {
        perror("映射失败:");
    }
    else
    {
        printf("映射成功\n");
    }

    read_JPEG_file(argv[1], map_addr, 0, 0);
    read_JPEG_file("3.jpg", map_addr, 679, 160);

    font *f = fontLoad("./simhei.ttf");
    if (f == NULL)
    {
        perror("加载字库失败\n");
        return -1;
    }

    fontSetSize(f, 24);
    bitmap *bm = createBitmapWithInit(80, 30, 4, 0x0000ff00);
    fontPrint(f, bm, 0, 0, "账号：", 0xff000000, 200);
    show_font_to_lcd(map_addr, 50, 200, bm);

    fontSetSize(f, 24);
    bitmap *bm1 = createBitmapWithInit(80, 30, 4, 0x0000ff00);
    fontPrint(f, bm1, 0, 0, "密码：", 0xff000000, 200);
    show_font_to_lcd(map_addr, 50, 240, bm1);

    fontSetSize(f, 24);
    bitmap *bm2 = createBitmapWithInit(80, 30, 4, 0x0000ff00);
    fontPrint(f, bm2, 0, 0, "登陆", 0xff000000, 200);
    show_font_to_lcd(map_addr, 200, 280, bm2);

    fontSetSize(f, 24);
    bitmap *bm3 = createBitmapWithInit(240, 30, 4, 0x0000ff00);
    fontPrint(f, bm3, 0, 0, "", 0xff000000, 200);
    show_font_to_lcd(map_addr, 140, 200, bm3);

    fontSetSize(f, 24);
    bitmap *bm4 = createBitmapWithInit(240, 30, 4, 0x0000ff00);
    fontPrint(f, bm4, 0, 0, "", 0xff000000, 200);
    show_font_to_lcd(map_addr, 140, 240, bm4);

    chumo(map_addr,f);
}